
import {  useVueFlow } from '@vue-flow/core'
import { ref } from 'vue';

const dragging = ref(false);
let draggingLineSourceHandleId:string = '';
let draggingLineSourceNodeId:string = '';
let draggingLineSourceHandletype:string = '';
function setDraggingLineSourceHandleId(nodeId:string, handleId:string, handleType:string){
  draggingLineSourceHandleId = handleId;
  draggingLineSourceNodeId = nodeId;
  draggingLineSourceHandletype = handleType;
  dragging.value = true;
}
function unsetDraggingLineSourceHandleId(){
  draggingLineSourceHandleId = '';
  draggingLineSourceNodeId = '';
  draggingLineSourceHandletype = '';
  dragging.value = false;
}

export function isValidConnection(nodeId:string, handleId:string, handleType:string):boolean {
    if(!dragging.value) return true;
    let flag = false;
    if (draggingLineSourceHandleId.includes('flow') && handleId.includes('flow')) {
      flag = true;
    }
    if (draggingLineSourceHandleId.includes('msg') && handleId.includes('msg')) {
      flag = true;
    }
    if (draggingLineSourceHandleId.includes('tool') && handleId.includes('tool')) {
      flag = true;
    }
    if (draggingLineSourceHandleId.includes('function') && handleId.includes('function')) {
      flag = true;
    }
    if (draggingLineSourceHandleId.includes('audio') && handleId.includes('audio')) {
      flag = true;
    }
    if (draggingLineSourceHandleId.includes('image') && handleId.includes('image')) {
      flag = true;
    }
    flag = flag && (draggingLineSourceHandletype != handleType);
    return flag;
    
}

export const useConnectionValid = () => {
    return { isValidConnection,setDraggingLineSourceHandleId, unsetDraggingLineSourceHandleId  }
}
export default useConnectionValid;
